<template>
  <div class="cart">
    <my-header myheader="购物车"></my-header>
    <div class="empty-cart" v-if="Cartlist.length===0">
      <img src="" alt="" class="empty-cart-img">
      <div class="empty-cart-text">购物车还是空的</div>
      <div class="btn" @click="jumpto('goodslist')">去逛逛</div>
    </div>
   <div class="infoData" v-else>
      <div class="cart-container" v-for="(itme,index) in Cartlist" :key="index">
        <span class="iconfont toogle" :class="itme.state ? 'icon-checkbox-marked-circ' : 'icon-checkbox-blank-circle-outline'" @click="statejump(index)"></span>
        <div class="item-detail">
          <img :src="itme.thumb_path" alt="">
          <div class="item-info">
            <h3>{{itme.title}}</h3>
            <div class="pay">
              <div class="pay-price">￥{{itme.sell_price}}</div>
              <div class="edit-quantity">
                <p class="operate-btn iconfont icon-minus" @click="jumpNum(index,-1,'minus')"></p>
                <p class="btn-input">{{itme.cou}}</p>
                <p class="operate-btn iconfont icon-plus" @click="jumpNum(index,1,'plus')"></p>
              </div>
            </div>
          </div>
        </div>
       </div>
      <div class="cart-footer">
        <div class="cart-footer-left"><span class="iconfont toogle" :class="isstateAll?'icon-checkbox-marked-circ' : 'icon-checkbox-blank-circle-outline'" @click="stateAll()"></span><span>全选</span></div>
        <div class="cart-footer-center"><span>合计：</span><span class="total-price">￥ {{statepriceAll}}</span></div>
        <div class="cart-footer-right"><span class="goto-pay">结算{{stateNumAll>0?`(${stateNumAll})`:''}}</span></div>
      </div>
   </div>
  </div>
</template>

<script>
import MyHeader from '@/components/myheader'
import { MessageBox } from 'mint-ui'
export default {
  data () {
    return {
      Cartlist: []
    }
  },

  components: {
    MyHeader
  },

  computed: {
    // 计算总数
    stateNumAll () {
      let totle = 0
      this.Cartlist.map(itme => {
        if (itme.state) {
          totle = itme.cou + totle
        }
      })
      return totle
    },
    // 计算选中商品的总价格
    statepriceAll () {
      let price = 0
      this.Cartlist.map(itme => {
        if (itme.state) {
          price = price + itme.cou * itme.sell_price
        }
      })
      return price
    },
    // 通过子选项切换全选按钮
    isstateAll () {
      let Allstate = true
      this.Cartlist.map(itme => {
        if (!itme.state) {
          Allstate = false
        }
      })
      return Allstate
    }
  },
  beforeDestroy () {
    // console.log('销毁前哦')
    localStorage.setItem('cartinfo', JSON.stringify(this.Cartlist))
  },
  mounted () {
    this.Cartlist = JSON.parse(localStorage.getItem('cartinfo') || '[]')
  },

  methods: {
    // 切换单个商品状态
    statejump (inx) {
      this.Cartlist[inx].state = !this.Cartlist[inx].state
    },
    // 全部商品状态改变单个商品状态
    stateAll () {
      let isstate = this.isstateAll
      this.Cartlist.map(itme => {
        itme.state = !isstate
      })
    },
    // 加减商品数量
    jumpNum (inx, num, type) {
      // 判断按下减号的时候是否只有一个了
      if (this.Cartlist[inx].cou === 1 && type === 'minus') {
        MessageBox.confirm('确定要删除此商品吗').then(action => {
          this.Cartlist.splice(inx, 1)
          return false
        }).catch(err => {
          console.log(err)
        })
      } else {
        this.Cartlist[inx].cou += num
      }
    },
    // 页面跳转
    jumpto (name) {
      this.$router.push({name: name})
    }
  }
}

</script>
<style lang='scss' scoped>
.cart {
  height: 320px;
  font-size: 14px;
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
    .empty-cart-img {
      height: 90px;
      width: 90px;
    }
    .empty-cart-text {
      font-size: 20px;
      color: #999;
      padding: 15px 0;
    }
    .btn {
      font-size: 20px;
      padding: 15px 55px;
      text-align: center;
      margin: 0 auto;
      border-radius: 10px;
      background: #ed601b;
      color: #fff;
    }
  }
  .cart-container {
    display: flex;
    align-items: center;
    margin: 5px;
    padding: 8px 10px;
    border: 1px solid #eeeeee;
    border-radius: 20px;
    .toogle {
      width: 20px;
      height: 20px;
      line-height: 20px;
      font-size: 20px;
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
  .item-detail {
    display: flex;
    flex: 1;
    img {
      margin: 2px 0 2px 5px;
      width: 90px;
      height: 90px;
    }
    .item-info {
      width: 100%;
      padding: 5px 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      h3 {
        font-size: 16px;
        font-weight: 400;
      }
      .pay {
        display: flex;
        justify-content: space-between;
        .pay-price {
          color: #ff5500;
          font-weight: bold;
          line-height: 30px;
        }
        .edit-quantity {
          display: flex;
          height: 30px;
          line-height: 30px;
          .operate-btn {
            padding: 0 10px;
            font-size: 20px;
            color: #bababa;
          }
          .btn-minus {
            font-size: 14px;
          }
        }
      }
    }
  }
  .cart-footer {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 50px;
    line-height: 50px;
    width: 100%;
    border-top: 1px solid #e7e7e7;
    background-color: #fff;
    .cart-footer-left {
      display: flex;
      justify-content: cneter;
      align-items: center;
      span {
        display: block;
        height: 50px;
        padding: 0 5px;
      }
    }
    .cart-footer-center {
      .total-price {
        color: #ff5500;
        font-weight: bold;
      }
    }
    .cart-footer-right {
      display: flex;
      flex-direction: column;
      padding: 0 20px;
      background-color: #ff5500;
      .goto-pay {
        color: #fff;
      }
    }
    .icon-checkbox-marked-circ {
      color: #ff5500;
    }
    .icon-checkbox-blank-circle-outline {
      color: #666666;
    }
  }
}
</style>
